package P150;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author LiangMu
 * @version \$Id: P00.java, v 0.1, 2024年07月06日 14:54 LiangMu Exp $$
 */
public class P00 {

    public static void main(String[] args) throws IOException {
        String path = "/Users/liangyuanliu/Desktop/docments/gitee/leetcode/src/main/java/P150";
        String temp = "package P150;\n\npublic class %s {\n}";
        File file = new File(path);
        String nextFileName = getNextFileName(file);
        File nextFile = new File(path + "/" + nextFileName + ".java");
        boolean newFile = nextFile.createNewFile();
        if (newFile) {
            FileWriter fileWriter = new FileWriter(nextFile);
            String str = String.format(temp, nextFileName);
            fileWriter.write(str);
            fileWriter.close();
        }
    }


    private static String getNextFileName(File file) {
        File[] files = file.listFiles();
        assert files != null;
        String lastName = Arrays.stream(files)
            .map(File::getName)
            .max(String::compareTo)
            .get();
        String p = lastName.split("\\.")[0].replaceAll("P", "");
        int lastIndex = Integer.parseInt(p);
        lastIndex++;
        return "P" + (lastIndex < 10 ? "0" + lastIndex : lastIndex);
    }

}
